// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-08-13

package src

import "github.com/kataras/iris/v12"

// Form
// read data from a form that formatted as application/x-www-form-urlencoded.
func Form(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        err = Operator.StructWithReader(v, ctx.ReadForm)
    } else {
        err = Operator.Struct(v)
    }
    return
}

// FormWithQuery
// read data from url string and form that formatted as
// application/x-www-form-urlencoded.
func FormWithQuery(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        err = Operator.StructWithReader(v, ctx.ReadQuery, ctx.ReadForm)
    } else {
        err = Operator.Struct(v)
    }
    return
}

// Json
// read data from a form that formatted as application/json.
func Json(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        if err = ctx.ReadJSON(v); err == nil {
            err = Operator.Struct(v)
        }
    } else {
        err = Operator.Struct(v)
    }
    return
}

// JsonWithQuery
// read data from url string and form that formatted as application/json.
func JsonWithQuery(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        if err = ctx.ReadQuery(v); err == nil {
            if err = ctx.ReadJSON(v); err == nil {
                err = Operator.Struct(v)
            }
        }
    } else {
        err = Operator.Struct(v)
    }
    return
}

// Query
// read data from url string.
func Query(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        err = Operator.StructWithReader(v, ctx.ReadQuery)
    } else {
        err = Operator.Struct(v)
    }
    return
}

// Xml
// read data from a form that formatted as application/xml.
func Xml(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        err = Operator.StructWithReader(v, ctx.ReadXML)
    } else {
        err = Operator.Struct(v)
    }
    return
}

// XmlWithQuery
// read data from url string and form that formatted as application/xml.
func XmlWithQuery(ctx iris.Context, v any) (err error) {
    if ctx != nil {
        err = Operator.StructWithReader(v, ctx.ReadQuery, ctx.ReadXML)
    } else {
        err = Operator.Struct(v)
    }
    return
}
